<template>
  <el-select v-model="sval" :class="className" :popper-class="popperClass" clearable placeholder="请选择计划" :style="{width}" @focus="onFocus" @change="onChcange">
    <el-option v-for="dt in lists" :key="dt.plantingPlanId" :value="dt.plantingPlanId" :label="dt.name" />
  </el-select>
</template>
<script setup>
import { ref, onMounted, watch, nextTick } from 'vue'
import { getSelectPlantList,getPlantList } from '@/api/plant'
import { useUserStore } from '@/pinia/modules/user'

const props = defineProps({
  modelValue: '',
  width: {
    type: String,
    default: '100%'
  },
  params: { // 查询条件
    type: Object,
    default: {}
  },
  className: {
    type: String,
    default: 'small-el-input__inner'
  },
  popperClass: {
    type: String,
    default: 'samll-el-select-dropdown__list'
  },
  optLists: '',
})
const emit = defineEmits(['update:modelValue', 'change','focus'])
const sval = ref('')
const lists = ref('')
const userStore = useUserStore()

onMounted(() => {
  // getTableData()
  if (props.optLists) {
    lists.value = props.optLists
    sval.value = props.modelValue || '' // 设置默认选项
  } else {
    getTableData()
  }
})

watch(() => props.modelValue, (n, o) => {
  sval.value = n
})
watch(() => props.optLists, (n, o) => {
  lists.value = n
})

const getTableData = async(params) => {
  const pm = params || props.params
  console.log({ page: 1, limit: 1000000, ...pm }, '==========')
  const res = await getPlantList({ page: 1, limit: 1000000, queryEntry:2,farmId: userStore.userInfo.farmId, ...pm })
  if (res.code == 0) {
    lists.value = res.data
    // console.log("GGGGG:",props)
    sval.value = props.modelValue || '' // 设置默认选项
  }
}

const onChcange = () => {
  emit('update:modelValue', sval.value)
  // emit('change',sval.value)
  if (sval.value) {
    emit('change', lists.value.find(ele => ele.farmId == sval.value))
  } else {
    emit('change', {})
  }
}
const onFocus = () => {
    emit('focus', {})
}


defineExpose({ getTableData })

</script>

<style scoped lang="scss">
</style>
